﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Slam.Configuration;
using Slam;
using Microsoft.SharePoint;
using Slam.SharePoint;
using System.Web;

namespace SlamCms.SharePoint.Core.Extensions
{
	public static class SPListItemExtensions
	{

		public static SlamCms.Data.ContentType GetSlamCmsContentType(this SPListItem listItem)
		{
			IConfigurationManager slamConfiguration = ComponentService.GetInstance<IConfigurationManager>();
			DataMapping currentDataMapping = slamConfiguration.DataMapping;
			string siteCollection = listItem.Web.Site.ServerRelativeUrl.TrimStart('/');
			if (!String.IsNullOrEmpty(siteCollection) && slamConfiguration.DataMappings.ContainsKey(siteCollection))
			{
				currentDataMapping = slamConfiguration.DataMappings[siteCollection];
			}

			ConfiguredType type = listItem.GetSlamConfiguredType();

			string contentTypeName = currentDataMapping.GetTypeAttribute(type.Id, type.Name, "ContentItemType");
			if (String.IsNullOrEmpty(contentTypeName))
			{
				contentTypeName = type.Name;
			}

			return (from ct in SlamCmsContext.Data.ContentTypes
					where ct.Title.Equals(contentTypeName, StringComparison.OrdinalIgnoreCase)
					select ct).FirstOrDefault();

		   
		}

        public static bool CheckContentApproval(this SPListItem listitem)
        {
            bool isContentApprovalEnabled = listitem.ParentList.EnableModeration;

            return isContentApprovalEnabled;
        }

	}
}
